﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Douban.Core.Helpers;
using System.IO;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;

namespace Douban.Core.Settings
{
    public class SettingManager : ISettingManager
    {
        private const string SettingFileName = "settings";
        private bool _dirty;
        private IDictionary<string, IList<Action<string, object>>> _handlers;
        private SettingItems _initSettings;

        public SettingItems Settings { get; set; }

        public SettingManager()
        {
            _handlers = new Dictionary<string, IList<Action<string, object>>>();
            Load();
        }

        private void Load()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SettingItems));

            if (!AppFileHelper.Exists(SettingFileName))
            {
                AppFileHelper.Create(SettingFileName);
                Settings = new SettingItems();
                AppFileHelper.WriteFile(SettingFileName, stream => serializer.Serialize(stream, Settings));
            }
            else
            {
                Stream stream = AppFileHelper.ReadFile(SettingFileName);
                Settings = (SettingItems)serializer.Deserialize(stream);
                stream.Close();
            }

            _initSettings = Settings.Clone();
            Settings.PropertyChanged += Settings_PropertyChanged;
        }

        public void Register(string settingName, Action<string, object> handler)
        { 
            IList<Action<string, object>> settingHandlers;
            if (!_handlers.TryGetValue(settingName, out settingHandlers))
            {
                settingHandlers = new List<Action<string, object>>();
                _handlers.Add(settingName, settingHandlers);
            }
            settingHandlers.Add(handler);
        }

        public void Undo()
        {
            var initProperties = TypeDescriptor.GetProperties(_initSettings);
            foreach (PropertyDescriptor propDescriptor in initProperties)
            {
                propDescriptor.SetValue(
                    Settings, 
                    propDescriptor.GetValue(_initSettings));
            }
        }

        public void Save()
        {
            if (_dirty)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SettingItems));
                AppFileHelper.WriteFile(SettingFileName, stream => serializer.Serialize(stream, Settings));
                _dirty = false;
                _initSettings = Settings;
            }
        }

        private void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            _dirty = true;
            PropertyInfo propInfo = sender.GetType().GetProperty(e.PropertyName);
            object settingValue = propInfo.GetValue(sender, null);
            OnSettingChanged(e.PropertyName, settingValue);
        }

        private void OnSettingChanged(string settingName, object settingValue)
        {
            IList<Action<string, object>> handlers;
            if (_handlers.TryGetValue(settingName, out handlers))
                foreach (var handler in handlers)
                    handler(settingName, settingValue);
        }
    }
}
